(function(){

    //正反选
    var oShop = document.querySelectorAll(".shop-5-1 input")
    var oAll = document.querySelector("ul>li>input")

    oAll.onclick = function(){
        for(var i=0;i<oShop.length;i++){
            oShop[i].checked = this.checked
        }
       
    }
    
    for(let i=0;i<oShop.length;i++){ //遍历 
        oShop[i].onclick = function(){ 
            var flag = true;            // 
            for(var i=0;i<oShop.length;i++){
                if (!oShop[i].checked) {
                        flag = false
                }
            }
            oAll.checked = flag
        }
    }


    //添加数量
    var oBut1 = document.querySelectorAll(".shop-5-1 .sp-2 .sp-txt1")
    var oBut2 = document.querySelectorAll(".shop-5-1 .sp-2 .sp-txt2")
    var oBut3 = document.querySelectorAll(".shop-5-1 .sp-2 .sp-txt3")
    var subtotal = document.querySelectorAll(".shop-5-1 .shop5-txt2")
    var total = document.querySelector(".shop-6 .s6-4")
    var oRemove = document.querySelectorAll(".shop-5-1 .shop5-txt3")
    console.log(oRemove);
    var dHz = document.querySelectorAll(".shop-5-1")
    var oQk = document.querySelector(".qk")
    console.log(dHz);
    var data=[
        {dj:246.5,numb:1},  
        {dj:246.5,numb:1}

    ]
    gettotal()
    for(let i=0;i<oBut1.length;i++){
        oBut3[i].onclick = function(){
            data[i].numb++
            aaa(i)         
        }
    }
    for(let i=0;i<oBut1.length;i++){
        oBut1[i].onclick = function(){
            if (!(data[i].numb <= 0)) {
                data[i].numb--
                aaa(i)
            }                      
        }
    }
    for(let i=0;i<oRemove.length;i++){
        let e = 0
        oRemove[i].onclick = function(){
            dHz[i].style.display = "none"
        }
        
    }
    oQk.onclick = function(){
        for(let j=0;j<oRemove.length;j++){
            dHz[j].style.display = "none" 
        }
        
    }
   
    
    function gettotal(){
        let m = 0;
        for(let i=0;i<oBut1.length;i++){
             m += data[i].numb * data[i].dj
        }

        return m
    }       

    var aaa = function(index){
        oBut2[index].innerHTML = data[index].numb;
            subtotal[index].innerHTML = "￥"+ data[index].numb * data[index].dj
            total.innerHTML =gettotal()
    }
})()


/* $(function () {
    $.ajax({
        url:"/api/cartList",
        data:{uId:JSON.parse(localStorage.getItem("userinfo")).uId}
    }).done(function (res) {
        let { data } = res;

        let strHtml = '';
        data.forEach(el => {
            strHtml += `
            <tr>
                <td>
                    <input type="checkbox">
                </td>
                <td>
                    <img src=${el.cImg}>${el.pname}
                </td>
                <td>
                    ${el.pprice}
                </td>
                <td>
                    <button>-</button><button>${el.pNumber}</button><button>+</button>
                </td>
                <td>
                    ${el.pTotal}
                </td>
                <td>
                    <a class="del" cid=${el.cId}>删除</a>
                </td>
            </tr>
            `
        })
        
        $("#tab").html(strHtml);

    })
})

    $(function () {


        $("#tab").on("click",".del",function () {
            let cid = $(this).attr("cid")

            $.ajax({
                url:"/api/delete",
                type:"post",
                data:{ cid }
            }).then(function () {
                layer.msg(res.message)
                $(this).parents("tr").remove()
            })



        })
        $("#tab").on("click",".sub",function () {
            let num = $(this).next().val()
            if (num <= 1) {
                return
            }
            $(this).next().val(--num)

           


            let cid = $(this).parents("tr").chidren("td").last().chidren().attr("cid")
            $.ajax({
                url:"/api/modify",
                type:"post",
                data:{
                    pnum:num,
                    cid
                }
            }).then(function (res) {
                 layer.msg(res.message);    
            })



        })
        $("#tab").on("click",".sup",function () {
            let num = $(this).prev().val()
            if (num <= 1) {
                return
            }
        })
        $(this).prev().val(++num)


         //修改总额
         let price =  $(this).parents("tr").chidren("td").eq(2).text()
         $(this).parents("tr").chidren("td").eq(4).text(num * price)


        let cid = $(this).parents("tr").chidren("td").last().chidren().attr("cid")
            $.ajax({
                url:"/api/modify",
                type:"post",
                data:{
                    pnum:num,
                    cid
                }
            }).then(function (res) {
                 layer.msg(res.message);    
            })

    }) */

